<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7">
      <h2>欢迎使用良医智慧屏后台管理系统</h2>
      <el-image class="img-logo" :src="require('@/assets/logo.png')"></el-image>
      <p>如有疑问及问题</p>
      <p>请联系赵工：17373176207</p>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical">

      </el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="loginForm">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="authCode">
          <el-input v-model="loginForm.authCode" style="width: 170px;float: left"></el-input>
          <el-image :src="authCodeImg" class="authCodeImg" @click="getAuthCode"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import qs from "qs";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        authCode: '',
        token: ''
      },
      rules: {
        userName: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        passWord: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '密码长度至少六位！', trigger: 'blur'}
        ],
        authCode: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '验证码长度为五位！', trigger: 'blur'}
        ]
      },
      authCodeImg: null
    };
  },
  methods: {
    //提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //res为返回结果，res = code + msg + data
          this.$axios.post('/login?' + qs.stringify(this.loginForm)).then(res => {
            //获取后端返回结果的jwt认证
            const jwt = res.headers['authorization']
            //提交jwt到store中进行存储管理
            this.$store.commit('SET_TOKEN', jwt)
            //登录成功，返回首页
            this.$router.push("/index")
          })
        } else {
          console.log('提交失败！！');
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getAuthCode() {
      this.$axios.get('/auth/getAuthCode').then(res => {
        console.log(res.data.data.authCodeImg)
        this.loginForm.token = res.data.data.token
        this.authCodeImg = res.data.data.authCodeImg
        this.loginForm.authCode = ''
      })
    }
  },
  created() {
    this.getAuthCode()
  }
}
</script>

<style scoped>
.el-row {
  height: 100vh;
  display: flex;
  align-items: center;
  text-align: center;

}

.el-divider {
  height: 200px;
}

.loginForm {
  text-align: left;
}

.authCodeImg {
  float: left;
  margin-left: 10px;
  border-radius: 5px;
}


.img-logo {
  width: 180px;
  height: 180px;
  margin-top: 20px;
  margin-bottom: 10px;
}

</style>